﻿/**************************************************************************
创建日期:	2019/7/16 9:21:44     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
Copyright (c)  ZhCun.cn

描	述： 登录状态缓存处理
记	录： 2019.8.14 增加Redis的缓存机制
***************************************************************************/
using System;
using ZhCun.DBModel;
using ZhCun.WebUtils.Authorizes;
using ZhCun.WebUtils.Configs;

namespace ZhCun.WebUtils
{
    public interface ILoginState
    {
        /// <summary>
        /// 初始化缓存
        /// </summary>
        void InitCache();
        /// <summary>
        /// 创建新token
        /// </summary>
        string CreateToken(LoginUser user);
        /// <summary>
        /// 检查token是否存在
        /// </summary>
        bool CheckToken(string token, out LoginUser user);
        /// <summary>
        /// 获取指定token
        /// </summary>
        LoginUser Get(string token);
        /// <summary>
        /// 设置token
        /// </summary>
        /// <param name="users"></param>
        string Set(LoginUser user);
        /// <summary>
        /// 移除token
        /// </summary>
        bool Remove(string token);
        ///// <summary>
        ///// 刷新token过期时间
        ///// </summary>
        //bool ResetExpire(LoginUser tokenObj);

        string RefreshToken(string token);
    }

    public static class LoginState
    {
        static LoginState()
        {
            string stateType = CommonConfig.THIS.LoginStateType.ToLower();

            StateObject = stateType switch
            {
                "redis" => new LoginStateRedis(),
                "local" => new LoginStateLocal(),
                _ => new LoginStateMemory(),
            };
        }

        readonly static ILoginState StateObject;

        /// <summary>
        /// 创建新token
        /// </summary>
        public static string CreateToken(LoginUser user)
        {
            return StateObject.CreateToken(user);
        }

        /// <summary>
        /// 检查Token是否有效
        /// </summary>
        public static bool CheckToken(string token, out LoginUser user)
        {
            return StateObject.CheckToken(token, out user);
        }
        /// <summary>
        /// 获取LoginToken对象
        /// </summary>
        public static LoginUser Get(string token)
        {
            return StateObject.Get(token);
        }
        /// <summary>
        /// 设置缓存
        /// </summary>
        public static string Set(LoginUser token)
        {
            return StateObject.Set(token);
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        public static void Remove(string token)
        {
            StateObject.Remove(token);
        }
        /// <summary>
        /// 初始化Token，
        /// memory: 从数据库中异步获取所有未到期的token列表，并放入缓存(异步)        
        /// </summary>
        public static void InitCache()
        {
            StateObject.InitCache();
        }

        public static string RefreshToken(string token)
        {
            return StateObject.RefreshToken(token);
        }
    }
}